﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SharpMap.Styles;
using System.Windows.Data;
using System.Windows.Media;
using SharpMap.Rendering.Rendering2D;
using SharpMap.Wpf.Rendering;

using LocalResources = SharpMap.Presentation.Wpf.Resources;
using SharpMap.Presentation.Wpf;
namespace SharpMap.Wpf.Thematics
{
    public class CitiesTheme : DependencyObject,
        IWpfTheme
    {



        public IViewContext ViewContext
        {
            get { return (IViewContext)GetValue(ViewContextProperty); }
            set { SetValue(ViewContextProperty, value); }
        }
        public static readonly DependencyProperty ViewContextProperty =
            DependencyProperty.Register("ViewContext", typeof(IViewContext), typeof(CitiesTheme), new UIPropertyMetadata(default(IViewContext)));

        double WorldWidth
        {
            get
            {
                if (ViewContext != null)
                {
                    return ViewContext.WorldWidth;
                }
                return 0.0;
            }
        }

        #region ITheme Members
        VectorStyle _ps1m = new VectorStyle();
        VectorStyle _ps500k = new VectorStyle();
        VectorStyle _ps100k = new VectorStyle();
        VectorStyle _ps20k = new VectorStyle();
        VectorStyle _ps5k = new VectorStyle();
        VectorStyle _psSmall = new VectorStyle();

        private void InitializePoints( VectorStyle ps, System.Windows.Media.DrawingImage imageSource)
        {
            ps.Symbol = new WpfSymbol2D(new Size2D(8, 8), imageSource);
            
            ps.SelectSymbol = new WpfSymbol2D(new Size2D(8, 8),
            LocalResources.DefaultSelectedSymbol);

            ps.HighlightSymbol = new WpfSymbol2D(new Size2D(8, 8),
            LocalResources.DefaultHighlightedSymbol);
            if (!imageSource.IsFrozen)
            {
                RenderOptions.SetEdgeMode(imageSource, EdgeMode.Aliased);
                imageSource.Freeze();
            }
        }

        public CitiesTheme()
            :this(null)
        { }
        public CitiesTheme(IViewContext context)
        {
            ViewContext = context;
            _ps500k.MaxVisible = 50.0;
            _ps100k.MaxVisible = 25;
    
            _ps20k.MaxVisible = 15;
    
            _ps5k.MaxVisible = 10;
    
            _psSmall.MaxVisible = 10;

            InitializePoints(_ps1m, LocalResources.City1M);
            InitializePoints(_ps500k,LocalResources.City500K);
            InitializePoints(_psSmall,LocalResources.CitySmall);
            InitializePoints(_ps5k, LocalResources.City5K);
            InitializePoints(_ps20k,LocalResources.City20K);
            InitializePoints(_ps100k,LocalResources.City100K);
        }
        public SharpMap.Styles.IStyle GetStyle(SharpMap.Data.IFeatureDataRecord attribute)
        {
            short popRank = attribute.GetInt16(7);
            SharpMap.Styles.IStyle style = GetSymbol(popRank);
            style.Enabled = style.MaxVisible >= WorldWidth
                && style.MinVisible <= WorldWidth;
            return style;
        }
  



        VectorStyle GetSymbol(short popRank)
        {
            if(popRank <= 2)
            {
                return _ps1m;
            }
            if (popRank <= 4)
            {
                return _ps500k;
            }

            if (popRank <= 6)
            {
                return _ps20k;
            }

            if (popRank <= 7)
            {
                return _ps5k;
            }
            else
            {
                return _psSmall;
            }
        }

        #endregion
    }
}
